local DateActHolidayGift = require("activity.date.HolidayGift")
local DateActChongzhiXBackY = require("activity.date.ChongzhiXBackY")
local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")
local ActDefine = require("activity.Define")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local OperateActFW = require("activity.OperateActFW")
local ItemDefine = require("bag.ItemDefine")

function onGetHolidayGiftData(human)
    DateActHolidayGift.onProecss(human)
end

function onGetHolidayGiftReward(human)
    DateActHolidayGift.onGetReward(human)
end

function onHolidayGiftReset()
    DateActHolidayGift.onActReset()
end

function onGetChongzhiXBackYData(human)
    DateActChongzhiXBackY.onProecss(human)
end

function onGetChongzhiXBackYReward(human)
    DateActChongzhiXBackY.onGetReward(human)
end


CRIT_DRAW_RECORD = CRIT_DRAW_RECORD or {}
CHONGZHI_DRAW_RECORD = CHONGZHI_DRAW_RECORD or {}
QPOINT_RECORD = QPOINT_RECORD or {}

function sendChongzhiRecord(human)
    local sendMsg = Msg.gc.GC_DATE_DRAW_RECORD
    sendMsg.info[0] = #CHONGZHI_DRAW_RECORD
    local cache = {}
    for i=1, #CHONGZHI_DRAW_RECORD do
        sendMsg.info[i].name = CHONGZHI_DRAW_RECORD[i].name
        Grid.create(cache, CHONGZHI_DRAW_RECORD[i].id, CHONGZHI_DRAW_RECORD[i].cnt)
        Grid.makeItem(cache, sendMsg.info[i].item, 0, human)
    end
    sendMsg.id = ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW
    Msg.send(sendMsg, human.fd)
end

function sendCritRecord(human)
    local sendMsg = Msg.gc.GC_DATE_DRAW_RECORD
    sendMsg.info[0] = #CRIT_DRAW_RECORD
    local cache = {}
    for i=1, #CRIT_DRAW_RECORD do
        sendMsg.info[i].name = CRIT_DRAW_RECORD[i].name
        Grid.create(cache, CRIT_DRAW_RECORD[i].id, CRIT_DRAW_RECORD[i].cnt)
        Grid.makeItem(cache, sendMsg.info[i].item, 0, human)
    end
    sendMsg.id = ActDefine.OPERATEACT_TYPE_CRITDRAW
    Msg.send(sendMsg, human.fd)
end

function sendQPointRecord(human)
    local sendMsg = Msg.gc.GC_DATE_DRAW_RECORD
    sendMsg.info[0] = #QPOINT_RECORD
    local cache = {}
    for i=1, #QPOINT_RECORD do
        sendMsg.info[i].name = QPOINT_RECORD[i].name
        Grid.create(cache, ItemDefine.ITEM_ID_YUANBAO, QPOINT_RECORD[i].cnt)
        Grid.makeItem(cache, sendMsg.info[i].item, 0, human)
    end
    sendMsg.id = ActDefine.OPERATEACT_TYPE_QPOINT
    Msg.send(sendMsg, human.fd)
end

function sendRecord(human, oType, info)
    if info.type == ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW then
        sendChongzhiRecord(human)
    elseif info.type == ActDefine.OPERATEACT_TYPE_CRITDRAW  then
        sendCritRecord(human)
    elseif info.type == ActDefine.OPERATEACT_TYPE_QPOINT then
        sendQPointRecord(human)
    end
end

function clearRecord()
  CRIT_DRAW_RECORD = {}
  CHONGZHI_DRAW_RECORD = {}
  QPOINT_RECORD = {}
end

function setRecord(human, oType, info)
    local record = {}
    local len = 0
    if info.type == ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW then
        record = CHONGZHI_DRAW_RECORD
        len = math.min(#record, 4)
    elseif info.type == ActDefine.OPERATEACT_TYPE_CRITDRAW  then
        record = CRIT_DRAW_RECORD
        len = math.min(#record, 14)
    elseif info.type == ActDefine.OPERATEACT_TYPE_QPOINT then
        record = QPOINT_RECORD
        len = math.min(#record, 5)
    end
    for i=len, 1, -1 do
        record[i + 1] = record[i]
    end
    record[1] = {}
    record[1].name = human.name
    record[1].id = info.id
    record[1].cnt = info.cnt
end

function setLotteryData()
  OperateActFW.onSetActDBData(ActDefine.OPERATEACT_TYPE_QPOINT, QPOINT_LOTTERY)
end

function getLotteryData()
  QPOINT_LOTTERY = OperateActFW.onGetActDBData(ActDefine.OPERATEACT_TYPE_QPOINT)
  if not QPOINT_LOTTERY then
    QPOINT_LOTTERY = {}
  end
end

